package gameobjects;

public class Weight extends Element {
 
	
        public void move(Hero hero) {
            int weightPositionX = this.getBox().getPositionX();
            int weightPositionY = this.getBox().getPositionY();
            int targetPositionY = weightPositionY-1;
            int offset = 0;
            //search until what position the beer can move, this is clean terrain
            while((hero.getCurrentBoard().getBoxes().getBox(weightPositionX, targetPositionY).getElement().getId()==Element.CleanTerrainId)){
                offset++;
                targetPositionY = targetPositionY-1;
            }
            targetPositionY = weightPositionY-offset;
            //if under the last clean terrain is the hero, then this die
            if((hero.getBox().getPositionX()==weightPositionX)&&(hero.getBox().getPositionY()==targetPositionY-1)){
                hero.getCurrentBoard().getGame().subtractALife();
            }
            //if not and offset is at least 1, swap positions
            else{
                //introduce the weight box into the updated boxes
                hero.getCurrentBoard().getUpdatedBoxes().add(hero.getCurrentBoard().getBoxes().getBox(weightPositionX, weightPositionY));
                if(offset>0){
                    //introduce the target box into the updated boxes
                    hero.getCurrentBoard().getUpdatedBoxes().add(hero.getCurrentBoard().getBoxes().getBox(weightPositionX, targetPositionY));
                    hero.getCurrentBoard().getBoxes().swapBoxes(weightPositionX, weightPositionY, weightPositionX, targetPositionY);
                }
            }
            
	}
	 
}
 
